<?php
/**
 * 一个简单的天气类，使用 cURL 扩展来请求数据
 **/
class Weather {
    // 请求地址
    private $url = "http://weather.yahooapis.com/forecastrss?w=12761782&u=c";
    private $weather;
    private $channel;
    private $details;
    private $forecast;
    private $climate;
 
    public function __construct() {
	$this->getWeather();
    }
	 public function getdata() {
	// 返回 json 数据
	return json_encode($this->climate\:Weather\:private);
    }
    private function getWeather() {
        // 初始化 cURL 对象
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$this->url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	$this->details = curl_exec($ch);
	curl_close($ch);
        // 解析 XML 
	$this->weather = simplexml_load_string($this->details);
	$this->channel = $this->weather->channel->children("http://xml.weather.yahoo.com/ns/rss/1.0");
	foreach ($this->channel as $i => $item) {
	    if ($i == 'location' || $i == 'astronomy') {
		foreach ($item->attributes() as $j => $attr) {
		    $this->climate[$i][$j] = "$attr";
		}
	    }
	}
	$this->forecast = $this->weather->channel->item->children("http://xml.weather.yahoo.com/ns/rss/1.0");
	foreach ($this->forecast as $i => $item) {
	    foreach ($item->attributes() as $j => $attr) {
		$this->climate[$i][$j] = "$attr";
	    }
	}
        
    }
}
$w = new Weather();
$data = $w->getdata();
print_r($w);

print_r($data);

?>